package de.ars.client.tools;

import java.io.IOException;
import java.io.InputStream;

import javax.microedition.io.Connector;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

import com.sun.lwuit.Button;
import com.sun.lwuit.Command;
import com.sun.lwuit.Component;
import com.sun.lwuit.Container;
import com.sun.lwuit.Form;
import com.sun.lwuit.RadioButton;
import com.sun.lwuit.TextArea;
import com.sun.lwuit.layouts.BorderLayout;

import de.ars.client.Main;
import de.ars.client.datahandling.PropertyReader;
import de.ars.common.IDataModelConstants;

/**
 * @author Dustin Augstein
 *
 */

public class Factory 
{
	
	public static TextArea createTextArea(String text)
	{
        return createTextArea(text,false,true);
	}
	
	public static TextArea createTextArea(String text,boolean focusable)
	{
        return createTextArea(text,focusable,true);
	}
	
	public static TextArea createTextArea(String text,boolean focusable,boolean drawBorder)
	{
		 TextArea result = new TextArea(text);
		 if (!drawBorder)
			 result.getStyle().setBorder(null);
		 result.setFocusable( focusable );
		 result.setEditable( true );
        return result;
	}
	
	public static RadioButton createRadioButton(String text)
	{
		RadioButton result = new RadioButton(text);
		 result.getStyle().setBorder(null);
        return result;
	}
	
	public static Button createButton(String text)
	{
		return createButton(text,true);
	}
	
	public static Button createButton(String text,boolean drawBorder)
	{
		Button result = new Button(text);
		if (!drawBorder)
			result.getStyle().setBorder(null);
		return result;
	}
	
	public static void addExitButton(Form form)
    {
        form.addCommand(new Command( PropertyReader.getProperty( "menu.exit" ), IDataModelConstants.EXIT_COMMAND ));
    }
	
	public static void addNextButton(Form form)
    {
        form.addCommand(new Command( PropertyReader.getProperty( "menu.next" ), IDataModelConstants.NEXT_COMMAND ));
    }
	
	public static void addBackButton(Form form)
    {
        form.addCommand(new Command( PropertyReader.getProperty( "menu.back" ), IDataModelConstants.BACK_COMMAND ));
    }
	
	public static void addEditButton(Form form)
	{
		form.addCommand(new Command(PropertyReader.getProperty( "menu.settings" ), IDataModelConstants.SETTINGS_COMMAND));
	}
	
	public static Container createInfoPair(String name,Component info,Main parent)
    {
        Container result = new Container(new BorderLayout());
        TextArea tap_name = createTextArea(name,false,false);
        tap_name.setPreferredW( parent.getFont( 0 ).stringWidth( name ) +10 );
        tap_name.setPreferredH( 10 );
        result.addComponent(BorderLayout.WEST,tap_name);
        result.addComponent(BorderLayout.CENTER,info);
        return result;//hm kk 
        //erstelle mal selbst ein png-image und lade das rein mom
    }
	
	public static Image createImage(String path)
	{
	    Image result = null;
	    try
        {
            result = Image.createImage( path );
        }
        catch( IOException e )
        {
        	System.out.println(path);
            e.printStackTrace();
        }
	    return result;
	}
	
	public static Image createImageFromFileSystem( String name )
	{
		Image img = null;
		try
		{
			 InputStream is = Connector.openInputStream( "file:///" + name );
			 int read;
			 byte readData[] = null;
			 byte[] buffer = new byte[1000];
			 while( (read = is.read(buffer)) != -1 )
			 {
			  if( readData == null )
			  {
			   readData = new byte[read];
			   System.arraycopy( buffer, 0, readData, 0, read );
			  }
			  else
			  {
			   byte tmp[] = new byte[readData.length + read];
			   System.arraycopy( readData, 0, tmp, 0, readData.length );
			   System.arraycopy( buffer, 0, tmp, readData.length, read );
			   readData = null; // suggest to the gc that we don't need it!
			   readData = tmp;
			   tmp = null; // suggest to the gc that we don't need it!
			  }
			 }
			 is.close();
			 img = Image.createImage( readData, 0, readData.length );
		}catch(Exception e ){}
	 return img;
	}
}
